<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/17
 * Time: 9:52
 */
namespace App\Http\Controllers\Api;

use App\Service\Api\AfterService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Constant\ErrorConstant;

class AfterController extends BaseController
{
    protected $service;

    public function __construct(Request $request, AfterService $service)
    {
        parent::__construct($request);
        $this->service  =   $service;
    }

    /**
     * 用户所有订单
     * @return \Illuminate\Http\JsonResponse
     */
    public function totalAfter()
    {
        $validator  =   Validator::make($this->request->post(), [
            'order_status'  =>  'required|integer|in:0,1,2,3,4,5'
        ], [
            'order_status.required' =>  '订单状态不能为空',
            'order_status.integer'  =>  '订单状态为整数',
            'order_status.in'       =>  '订单状态选择错误'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_AFTER_LIST_ERROR, $validator->errors()->first());
        }

        $page   =   $this->request->input('page', 1);
        $query  =   $this->request->post('order_status', 0);
        [$flag, $result]    =   $this->service->afterTotal($query, (int) $page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_AFTER_LIST_ERROR, $result);
        }
    }

    /**
     * 用户删除订单
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteAfter()
    {
        $validator  =   Validator::make($this->request->post(), [
            'order_number'  =>  'required|string|exists:mini_order,order_no'
        ], [
            'order_number.required' =>  '订单号不能为空',
            'order_number.string'   =>  '订单号必须为字符',
            'order_number.exists'   =>  '订单不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_AFTER_DELETE_ERROR);
        }

        $order_number   =   $this->request->post('order_number');

        [$flag, $result]=   $this->service->afterDelete($order_number);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_AFTER_DELETE_ERROR, $result);
        }
    }

    /**
     * 用户取消订单
     * @return \Illuminate\Http\JsonResponse
     */
    public function cancelAfter()
    {
        $validator  =   Validator::make($this->request->post(), [
            'order_number'  =>  'required|string|exists:mini_order,order_no',
            'cancel_reason' =>  'required|integer|in:1,2,3,4,5,6'
        ], [
            'order_number.required' =>  '订单号不能为空',
            'order_number.string'   =>  '订单号必须为字符',
            'order_number.exists'   =>  '订单不存在',
            'cancel_reason.required'=>  '取消原因不能为空',
            'cancel_reason.integer' =>  '取消原因必须为整数',
            'cancel_reason.in'      =>  '取消原因选择错误',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_AFTER_CANCEL_ERROR);
        }

        $order_number   =   $this->request->post('order_number');
        $cancel_reason  =   $this->request->post('cancel_reason');

        [$flag, $result]=   $this->service->afterCancel($order_number, (int) $cancel_reason);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_AFTER_CANCEL_ERROR, $result);
        }
    }

    /**
     * 用户确认订单
     * @return \Illuminate\Http\JsonResponse
     */
    public function confirmAfter()
    {
        $validator  =   Validator::make($this->request->post(), [
            'order_number'  =>  'required|string|exists:mini_order,order_no'
        ], [
            'order_number.required' =>  '订单号不能为空',
            'order_number.string'   =>  '订单号必须为字符',
            'order_number.exists'   =>  '订单不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_AFTER_CONFIRM_ERROR);
        }

        $order_number   =   $this->request->post('order_number');

        [$flag, $result]=   $this->service->afterConfirm($order_number);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_AFTER_CONFIRM_ERROR, $result);
        }
    }

    /**
     * 用户订单详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function detailAfter()
    {
        $validator  =   Validator::make($this->request->post(), [
            'order_number'  =>  'required|string|exists:mini_order,order_no'
        ], [
            'order_number.required' =>  '订单号不能为空',
            'order_number.string'   =>  '订单号必须为字符',
            'order_number.exists'   =>  '订单不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_AFTER_DETAIL_ERROR);
        }

        $order_number   =   $this->request->post('order_number');

        [$flag, $result]=   $this->service->afterDetail($order_number);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_AFTER_DETAIL_ERROR, $result);
        }
    }

    /**
     * 用户评价订单
     * @return \Illuminate\Http\JsonResponse
     */
    public function commentAfter()
    {
        $validator  =   Validator::make($this->request->post(), [
            'order_number'  =>  'required|string|exists:mini_order,order_no',
            'good_id'       =>  'required|integer|exists:mini_product,id',
            'seller_star'   =>  'required|integer|in:1,2,3,4,5',
            'good_star'     =>  'required|integer|in:1,2,3,4,5',
            'logistic_star' =>  'required|integer|in:1,2,3,4,5',
            'comment'       =>  'string',
            'comment_pic'   =>  'array',
        ], [
            'order_number.required' =>  '订单号不能为空',
            'order_number.string'   =>  '订单号必须为字符',
            'order_number.exists'   =>  '订单不存在',

            'good_id.required'      =>  '商品ID不能为空',
            'good_id.integer'       =>  '商品ID必须为整型',
            'good_id.exists'        =>  '商品不存在',

            'seller_star.required'  =>  '店铺服务评价不能为空',
            'good_star.required'    =>  '商品质量评价不能为空',
            'logistic_star.required'=>  '物流服务评价不能为空',
            'comment.required'      =>  '评论内容不能为空',

            'seller_star.integer'   =>  '店铺服务评价必须为整型',
            'good_star.integer'     =>  '商品质量评价必须为整型',
            'logistic_star.integer' =>  '物流服务评价必须为整型',

            'comment.string'        =>  '评论内容订单号必须为字符',

            'seller_star.in'        =>  '店铺服务评价选择错误',
            'good_star.in'          =>  '商品质量评价选择错误',
            'logistic_star.in'      =>  '物流服务评价选择错误',

            'comment_pic.array'     =>  '评价图片必须为数组'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_AFTER_COMMENT_ERROR, $validator->errors()->first());
        }

        $order_number   =   $this->request->post('order_number');
        $good_id        =   $this->request->post('good_id');

        [$flag, $result]=   $this->service->afterComment($this->request->post(), $order_number, (int) $good_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_AFTER_COMMENT_ERROR, $result);
        }
    }

    /**
     * 用户评价订单（同时多个商品）
     * @return \Illuminate\Http\JsonResponse
     */
    public function evaluateAfter()
    {
        $validator  =   Validator::make($this->request->post(), [
            'order_number'  =>  'required|string|exists:mini_order,order_no',
            'evaluate'      =>  'required|array',
            'evaluate.*.good_id'       =>  'required|integer|exists:mini_product,id',
            'evaluate.*.seller_star'   =>  'required|integer|in:1,2,3,4,5',
            'evaluate.*.good_star'     =>  'required|integer|in:1,2,3,4,5',
            'evaluate.*.logistic_star' =>  'required|integer|in:1,2,3,4,5',
            'evaluate.*.comment'       =>  'string',
            'evaluate.*.comment_pic'   =>  'array',
        ], [
            'order_number.required'    =>  '订单号不能为空',
            'order_number.string'      =>  '订单号必须为字符',
            'order_number.exists'      =>  '订单不存在',

            'evaluate.required'        =>  '评价不能为空',
            'evaluate.array'           =>  '评价必须为数组',

            'evaluate.*.good_id.required'      =>  '商品ID不能为空',
            'evaluate.*.good_id.integer'       =>  '商品ID必须为整型',
            'evaluate.*.good_id.exists'        =>  '商品不存在',

            'evaluate.*.seller_star.required'  =>  '店铺服务评价不能为空',
            'evaluate.*.good_star.required'    =>  '商品质量评价不能为空',
            'evaluate.*.logistic_star.required'=>  '物流服务评价不能为空',
            'evaluate.*.comment.required'      =>  '评论内容不能为空',

            'evaluate.*.seller_star.integer'   =>  '店铺服务评价必须为整型',
            'evaluate.*.good_star.integer'     =>  '商品质量评价必须为整型',
            'evaluate.*.logistic_star.integer' =>  '物流服务评价必须为整型',

            'evaluate.*.comment.string'        =>  '评论内容订单号必须为字符',

            'evaluate.*.seller_star.in'        =>  '店铺服务评价选择错误',
            'evaluate.*.good_star.in'          =>  '商品质量评价选择错误',
            'evaluate.*.logistic_star.in'      =>  '物流服务评价选择错误',

            'evaluate.*.comment_pic.array'     =>  '评价图片必须为数组'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_AFTER_COMMENT_ERROR, $validator->errors()->first());
        }

        $order_number   =   $this->request->post('order_number');
        $evaluate       =   $this->request->post('evaluate');

        [$flag, $result]=   $this->service->afterEvaluate($order_number, $evaluate);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_AFTER_COMMENT_ERROR, $result);
        }
    }

    /**
     * 用户修改订单地址
     * @return \Illuminate\Http\JsonResponse
     */
    public function addressAfter()
    {
        $validator  =   Validator::make($this->request->post(), [
            'order_number'  =>  'required|string|exists:mini_order,order_no',
            'address_id'    =>  'required|integer|exists:mini_user_address,id'
            /*'userName'      =>  'required|string|max:50',
            'provinceName'  =>  'required|string|max:50',
            'cityName'      =>  'required|string|max:50',
            'countyName'    =>  'required|string|max:50',
            'detailInfo'    =>  'required|string|max:100',
            'telNumber'     =>  'required|string|max:20',*/
        ], [
            'order_number.required' =>  '订单号不能为空',
            'order_number.string'   =>  '订单号必须为字符',
            'order_number.exists'   =>  '订单不存在',

            'address_id.required'   =>  '地址ID不能为空',
            'address_id.integer'    =>  '地址ID必须为整数',
            'address_id.exists'     =>  '地址ID不存在',

            'userName.required'     =>  '收货人姓名不能为空',
            'userName.string'       =>  '收货人姓名必须为字符',
            'userName.max'          =>  '收货人姓名过长',

            'provinceName.required' =>  '收货人省份不能为空',
            'provinceName.string'   =>  '收货人省份必须为字符',
            'provinceName.max'      =>  '收货人省份过长',

            'cityName.required'     =>  '收货人城市不能为空',
            'cityName.string'       =>  '收货人城市必须为字符',
            'cityName.max'          =>  '收货人城市过长',

            'countyName.required'   =>  '收货人地区不能为空',
            'countyName.string'     =>  '收货人地区必须为字符',
            'countyName.max'        =>  '收货人地区过长',

            'detailInfo.required'   =>  '收货人详细地址不能为空',
            'detailInfo.string'     =>  '收货人详细地址必须为字符',
            'detailInfo.max'        =>  '收货人详细地址过长',

        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_AFTER_ADDRESS_ERROR);
        }

        $order_number   =   $this->request->post('order_number');
        $address_id     =   $this->request->post('address_id');

        [$flag, $result]=   $this->service->afterAddress($address_id, $order_number);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_AFTER_ADDRESS_ERROR, $result);
        }
    }

    /**
     * 用户查看订单物流信息
     * @return \Illuminate\Http\JsonResponse
     */
    public function expressAfter()
    {
        $validator  =   Validator::make($this->request->post(), [
            'order_number'  =>  'required|string|exists:mini_order,order_no'
        ], [
            'order_number.required' =>  '订单号不能为空',
            'order_number.string'   =>  '订单号必须为字符',
            'order_number.exists'   =>  '订单不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_AFTER_EXPRESS_ERROR);
        }

        $order_number   =   $this->request->post('order_number');

        [$flag, $result]=   $this->service->afterExpress($order_number);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_AFTER_EXPRESS_ERROR, $result);
        }
    }

    /**
     * 订单继续支付
     * @return \Illuminate\Http\JsonResponse
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function continueAfter()
    {
        $validator  =   Validator::make($this->request->post(), [
            'order_number'  =>  'required|string|exists:mini_order,order_no'
        ], [
            'order_number.required' =>  '订单号不能为空',
            'order_number.string'   =>  '订单号必须为字符',
            'order_number.exists'   =>  '订单不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_AFTER_CONTINUE_ERROR);
        }

        $order_number   =   $this->request->post('order_number');

        [$flag, $result]=   $this->service->afterContinue($order_number);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_AFTER_CONTINUE_ERROR, $result);
        }
    }

}
